package com.example.hello;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;

public class MyList3MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    private static final String TAG = "MyList3MainActivity";
    ListView listView;
    RateAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list3_main);
        listView = findViewById(R.id.mylist3);

        ProgressBar progressBar = findViewById(R.id.progressBar);
        //progressBar.setVisibility(View.GONE);
/*
        ArrayList<HashMap<String,String>> listItems=new ArrayList<HashMap<String,String>>();
        for(int i=0;i<10;i++){
            HashMap<String,String> map=new HashMap<String,String>();
            map.put("ItemTitle","Rate:"+i);
            map.put("ItemDetail","detail"+i);
            listItems.add(map);
        }
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter=new SimpleAdapter(this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.item_title,R.id.item_detail});
        listView.setAdapter(listItemAdapter);
        listView.setVisibility(View.VISIBLE);

 */

        //HUOQUWANGLUOSHUJ
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 11) {
                    //ArrayList<RateItem> listItems=(ArrayList<RateItem>) msg.obj;
                    //ArrayList<String> list3=(ArrayList<String>)msg.obj;
                    /*SimpleAdapter listItemAdapter=new SimpleAdapter(MyList3MainActivity.this,
                            listItems,
                            R.layout.list_item,
                            new String[]{"country","rate"},
                            new int[]{R.id.item_title,R.id.item_detail});

                     */
                    //adapter=new MyAdapter(MyList3MainActivity.this,R.layout.list_item,listItems);
                    adapter = new RateAdapter(MyList3MainActivity.this, R.layout.list_item, (ArrayList
                            <Rate>) msg.obj);
                    listView.setAdapter(adapter);

                    //显示隐藏
                    listView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };
        RateTask task = new RateTask();
        task.setHandler(handler);
        Thread t = new Thread(task);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Object itemAtPosition = listView.getItemAtPosition(position);
        //HashMap<String, String> map = (HashMap<String, String>) itemAtPosition;
        Rate item = (Rate) listView.getItemAtPosition(position);

        //String countryStr = map.get("country");
        //String rateStr = map.get("rate");
        String countryStr = item.getCountry();
        String rateStr = item.getRate();

        Log.i(TAG, "onItemClick:countryStr" + countryStr);
        Log.i(TAG, "onItemClick:rateStr" + rateStr);
        // adapter.remove(map);

        show(countryStr, rateStr);

    }

    private void show(String countryStr, String rateStr) {
        Intent config = new Intent(this, CountRateActivity.class)
                .putExtra("count_country", countryStr)
                .putExtra("count_rate", rateStr);
        Log.i(TAG, "openCountRate:count_country=" + countryStr);
        Log.i(TAG, "openCountRate:count_rate=" + rateStr);
        //Log.i(TAG,"openConfig:dollarRate:",dollarRate);
        startActivity(config);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick:长按事件 position=" + position);
        Rate item = (Rate) listView.getItemAtPosition(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick:对话框事件处理");
                        //删除数据项
                        adapter.remove(item);
                    }
                }).setNegativeButton("否", null);
        builder.create().show();
        //adapter.remove(item);
        return true;//false:长按后会触发单击事件
    }
}